package kr.co.redcircle.service.oAuth.extractor;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.scribe.exceptions.OAuthException;
import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.model.Token;
import org.scribe.utils.OAuthEncoder;

public class TokenExtractorGoogle20Impl implements AccessTokenExtractor {
	
	private static final String EMPTY_SECRET = "";

	@Override
	public Token extract(String response) {
		
		try{
			JSONObject respObject = JSONObject.fromObject(response);
			String token = OAuthEncoder.decode(respObject.getString("access_token"));
			return new Token(token, EMPTY_SECRET, response);
		}catch(JSONException e){
			throw new OAuthException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null);
		}
		
	}

}
